package com.hex.ds.hdtp.core.app.license.truelicense.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Package com.hex.ds.hdtp.core.app.license.truelicense
 * @ClassName LicenseCheckInterceptor
 * @Description 添加拦截器，用于在登录的时候校验License证书
 * @Author Yin.Yang
 * @Date 2024/3/27 15:45
 * @Version v1.0
 **/
@Component
public class LicenseCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LicenseVerify licenseVerify = new LicenseVerify();

        //校验证书是否有效
        boolean verifyResult = licenseVerify.verify();

        if(verifyResult){
            return true;
        } else {
            response.setCharacterEncoding("utf-8");
            JSONObject obj = new JSONObject();
            obj.put("errcode", "0319");
            obj.put("errmsg", "您的证书无效，请核查服务器是否取得授权或重新申请证书！");
            response.getWriter().print(obj);
            response.getWriter().flush();
            return false;
        }
    }

}